package com.shuimo.emoswxapi.wx.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuimo.emoswxapi.wx.db.entity.TbUser;
import com.shuimo.emoswxapi.wx.db.mapper.TbUserMapper;
import com.shuimo.emoswxapi.wx.db.service.ITbUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 宋发准
 * @since 2021-09-14
 */
@Service
@Slf4j
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements ITbUserService {

    @Autowired
    private TbUserMapper userMapper;

    /**
     * root用户是否存
     *
     * @return
     */
    @Override
    public boolean haveRootUser() {
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbUser::getRoot, true);
        List<TbUser> list = list(wrapper);
        return list.size() > 0;
    }

    /**
     * 根据openid获取信息
     *
     * @param openId
     * @return
     */
    @Override
    public TbUser getByOpenId(String openId) {
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbUser::getStatus, 1);
        wrapper.eq(TbUser::getOpenId, openId);
        return getOne(wrapper);
    }

    /**
     * 查询用户权限
     *
     * @param userId
     * @return
     */
    @Override
    public Set<String> searchUserPermissions(int userId) {
        return userMapper.searchUserPermissions(userId);
    }
}
